package com.yuxi.controller.manager;
import com.yuxi.common.SkJsonResult;
import com.yuxi.common.SkList;
import com.yuxi.common.StringUtil;
import com.yuxi.enums.ProductTypeTypeFlagEnum;
import com.yuxi.model.ProductType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RestController;
import com.yuxi.pubInter.exception.SkException;
import com.yuxi.pubInter.IBaseService;
import com.yuxi.pubInter.RestfulController;
import com.yuxi.dto.ProductTypeDTO;
import com.yuxi.dto.ProductTypePageDTO;
import com.yuxi.modelExt.ProductTypeExt;
import com.yuxi.searchExt.ProductTypeSearchExt;
import com.yuxi.service.IProductTypeService;
import com.yuxi.vo.ProductTypeDetailVO;
import com.yuxi.vo.ProductTypeListVO;
import com.yuxi.vo.ProductTypeDomainVO;
import com.yuxi.vo.ProductTypeDetailDomainVO;

import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import com.yuxi.pubInter.exception.BusinessException;

/**
*    @author sunke
*    @Date 2023-10-13 10:28:19
*    @description ProductTypeController      产品分类
*/
@Api(tags = "后台管理系统-产品分类相关", description = "产品分类相关接口")
@RestController(value ="productTypeRestfulController")
@RequestMapping("/manager/productType/")
@Validated
public class ProductTypeController extends ApiManageController<ProductTypeDTO,ProductTypePageDTO,ProductTypeExt, ProductTypeSearchExt,ProductTypeDomainVO,ProductTypeDetailDomainVO,ProductTypeDetailVO,ProductTypeListVO,String> {

	@Resource(name = "productTypeService")
	private IProductTypeService productTypeService;

	@Override
	public void afterPostDelete(String id, HttpServletRequest request, HttpSession session) throws SkException {

		List<ProductTypeExt> productTypeExtList=productTypeService.getAllList(ProductTypeSearchExt.getInstance().setUpIdEq(id));
		if(StringUtil.isNotObjEmpty(productTypeExtList)&& productTypeExtList.size()>0){
			List<String> ids=new ArrayList<>();
			productTypeExtList.stream().forEach(productTypeExt -> {
				ids.add(productTypeExt.getId());
			});
			productTypeService.batchDelete(ids);
		}

		super.afterPostDelete(id, request, session);
	}

	@ApiOperation(value = "顶级产品分类", notes = "顶级产品分类")
	@PostMapping(value = "topProductList")
	public SkJsonResult<ProductTypeListVO> topProductList(@RequestBody ProductTypePageDTO pageDTO, HttpServletRequest request, HttpSession session) throws SkException {
		pageDTO.setTypeFlagIn(SkList.getInstance().addObjToList(ProductTypeTypeFlagEnum.PRODUCT_TYPE.getType()));
		pageDTO.setTopFlag(1);
		return super.list(pageDTO, request, session);
	}
	@ApiOperation(value = "顶级品牌产品分类", notes = "顶级品牌产品分类")
	@PostMapping(value = "topBrandtList")
	public SkJsonResult<ProductTypeListVO> topBrandtList(@RequestBody ProductTypePageDTO pageDTO, HttpServletRequest request, HttpSession session) throws SkException {
		pageDTO.setTypeFlagIn(SkList.getInstance().addObjToList(ProductTypeTypeFlagEnum.BRAND.getType()));
		pageDTO.setTopFlag(1);
		return super.list(pageDTO, request, session);
	}

	@Override
	public IBaseService<ProductTypeExt, ProductTypeSearchExt,String> getBaseService() throws SkException {
		return productTypeService;
	}

	@Override
	public String getBaseRoute() {
		return "/manager/productType/";
	}

	/**
	* 当前模板名称
	* @return
	*/
	@Override
	public String getModelName(){
		return "产品分类";
	}



	@Override
	public Set<String> totalMapSet() {
		Set totalMapSet=new HashSet();
		totalMapSet.add("count(product_type.pt_id) as countNum");
		return totalMapSet;
	}


}